<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@page pageEncoding="UTF-8"%>
<%@include file="../WEB-INF/jspf/controlesessao.jspf"%>
<c:catch var ="catchException">
    <sql:update var="delete" dataSource="jdbc/dbo_MicrosoftSQLServer">
        delete from formacoes
        where matricula=?
        ;
        delete turnos_docentes
        where matricula=?
        <sql:param value="${param.matricula}"/>
        <sql:param value="${param.matricula}"/>
    </sql:update>

    <sql:update var="insertT" dataSource="jdbc/dbo_MicrosoftSQLServer">
        insert into turnos_docentes(id_turno,matricula) values(?,?)
        <sql:param value="${param.id_turno}"/>
        <sql:param value="${param.matricula}"/>
    </sql:update>
    
    <c:catch var ="catchException">
        <sql:update var="insertD" dataSource="jdbc/dbo_MicrosoftSQLServer">
            delete docentes_complemento
            where matricula=?
            <sql:param value="${param.matricula}"/>
        </sql:update>

        <sql:update var="insertD" dataSource="jdbc/dbo_MicrosoftSQLServer">
            insert into docentes_complemento(matricula,email,ativo) values(?,?,?)
            <sql:param value="${param.matricula}"/>
            <sql:param value="${param.email}"/>
            <sql:param value="${param.ativo}"/>
        </sql:update>

    </c:catch>
    


    <c:forEach var='parameter' items='${paramValues}'>
        <c:if test="${fn:endsWith(parameter.key,'itens')}">
            <c:forEach var='value' items='${parameter.value}'>
                <c:set var="item" value="${value}"/>

                <c:set var="idcursosF" value="id_cursosF_${item}"/>
                <c:set var="pdca" value="pdca_${item}"/>
                <c:set var="valor" value="valor_${item}"/>
                <c:set var="dataInicio" value="data_inicio_${item}"/>
                <c:set var="dataFim" value="data_fim_${item}"/>
                <c:set var="descFormacao" value="desc_formacao_${item}"/>
                <c:set var="cargaHoraria" value="carga_horaria_${item}"/>
                <c:set var="instituicao" value="instituicao_${item}"/>

                <c:set var="fidcursosF" value="${param[idcursosF]}"/>
                <c:set var="fpdca" value="${param[pdca]}"/>
                <c:set var="fdataInicio" value="${param[dataInicio]}"/>
                <c:set var="fdataFim" value="${param[dataFim]}"/>
                <c:set var="fdescFormacao" value="${param[descFormacao]}"/>
                <c:set var="fcargaHoraria" value="${param[cargaHoraria]}"/>
                <c:set var="finstituicao" value="${param[instituicao]}"/>

                <fmt:parseNumber value="${param[valor]}" pattern="##,###.00#" var="fvalor"/>

                <fmt:parseDate var="dataInicioFrm" value="${fdataInicio}"/>
                <fmt:parseDate var="dataFimFrm" value="${fdataFim}"/>

                <sql:update var="insertC" dataSource="jdbc/dbo_MicrosoftSQLServer">
                    insert into formacoes(matricula,id_cursosF,pcda,valor,data_inicio,data_fim,desc_formacao,carga_horaria,instituicao)
                    values(?,?,?,?,?,?,?,?,?)
                    <sql:param value="${param.matricula}"/>
                    <sql:param value="${fidcursosF}"/>
                    <sql:param value="${fpdca}"/>
                    <sql:param value="${fvalor}"/>
                    <sql:dateParam value="${dataInicioFrm}"/>
                    <sql:dateParam value="${dataFimFrm}"/>
                    <sql:param value="${fdescFormacao}"/>
                    <sql:param value="${fcargaHoraria}"/>
                    <sql:param value="${finstituicao}"/>

                </sql:update>


            </c:forEach>
        </c:if>
    </c:forEach>
    <p>Registro Salvo com sucesso!</p>
</c:catch>
<c:if test = "${catchException!=null}">
    Erro : Não foi possível realizar a alteração.<br><br>
    ${catchException}
</c:if>
